﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace docent.Addin
{
    public partial class DocPane : UserControl
    {
        public DocPane()
        {
            InitializeComponent();

            ExpandDocuments();
        }

        private void ExpandDocuments()
        {
            tvDocuments.Nodes.Clear();

            FlowState state = FlowState.None;
            if (btnBeWritten.Checked)
                state |= FlowState.BeWritten;
            if (btnWriting.Checked)
                state |= FlowState.Writing;
            if (btnWritten.Checked)
                state |= FlowState.Written;
            if (btnBeReviewed.Checked)
                state |= FlowState.BeReviewed;
            if (btnReviewing.Checked)
                state |= FlowState.Reviewing;
            if (btnReviewed.Checked)
                state |= FlowState.Reviewed;

            TreeNode topNode = new TreeNode()
            {
                Name = "ALL",
                Text = "所有"
            };
            tvDocuments.Nodes.Add(topNode);

            Word.Word2007Documents documents = new Word.Word2007Documents();
            Document[] docs = null;
            docs = documents.QueryByState(-1, state);
            foreach (Document doc in docs)
            {
                InsertDocument(topNode, doc);
            }
            tvDocuments.ExpandAll();
        }

        private void InsertDocument(TreeNode node, Document doc)
        {
            TreeNode newnode = new TreeNode()
            {
                Name = doc.Name,
                Text = doc.Name + "[" + doc.FlowState.ToString() + "]",
                Tag = doc
            };
            node.Nodes.Add(newnode);
            foreach (Document sub in doc)
            {
                InsertDocument(newnode, sub);
            }
        }

        private void btnBeWritten_Click(object sender, EventArgs e)
        {
            ToolStripButton btn = sender as ToolStripButton;
            btn.Checked = !btn.Checked;
            ExpandDocuments();
        }

        private void btnWriting_Click(object sender, EventArgs e)
        {
            ToolStripButton btn = sender as ToolStripButton;
            btn.Checked = !btn.Checked;
            ExpandDocuments();
        }

        private void btnWritten_Click(object sender, EventArgs e)
        {
            ToolStripButton btn = sender as ToolStripButton;
            btn.Checked = !btn.Checked;
            ExpandDocuments();
        }

        private void btnBeReviewed_Click(object sender, EventArgs e)
        {
            ToolStripButton btn = sender as ToolStripButton;
            btn.Checked = !btn.Checked;
            ExpandDocuments();
        }

        private void btnReviewing_Click(object sender, EventArgs e)
        {
            ToolStripButton btn = sender as ToolStripButton;
            btn.Checked = !btn.Checked;
            ExpandDocuments();
        }

        private void btnReviewed_Click(object sender, EventArgs e)
        {
            ToolStripButton btn = sender as ToolStripButton;
            btn.Checked = !btn.Checked;
            ExpandDocuments();
        }
    }
}
